#include "StdAfx.h"
#include "PlugsConfigure.h"
#include "ContainerHelper.h"
#include "Resource.h"


#define NODE_ROOT _T("AnyCAD")
#define NODE_ALLEXPORTERS		_T("AllExporters")
#define NODE_ALLIMPORTERS		_T("AllImporters")
#define NODE_XPORTER			_T("Xporter")
#define NODE_XPORTER_NAME		_T("Name")
#define NODE_XPORTER_GUID		_T("GUID")
#define NODE_XPORTER_DESC		_T("Description")
#define NODE_XPORTER_EXT		_T("Extention")
#define NODE_XPORTER_DLL		_T("Dll")
#define NODE_XPORTER_AUTHOR		_T("Author")


CPlugsConfigure::CPlugsConfigure(void)
{
}

CPlugsConfigure::~CPlugsConfigure(void)
{

	ClearPtrContainer(m_Exporters);
	ClearPtrContainer(m_Importers);
}

void CPlugsConfigure::VisitExporterItem(IXportItemVisitor* pVisitor)
{
	XportItemList::const_iterator itlist = m_Exporters.begin();
	for(; itlist!= m_Exporters.end(); ++itlist)
	{
		pVisitor->OnVisitItem(*itlist);
	}
}
void CPlugsConfigure::VisitImporterItem(IXportItemVisitor* pVisitor)
{
	XportItemList::const_iterator itlist = m_Importers.begin();
	for(; itlist!= m_Importers.end(); ++itlist)
	{
		pVisitor->OnVisitItem(*itlist);
	}
}
bool CPlugsConfigure::Load(LPTSTR szPathFile)
{
	// parse xml document
	MSXML2::IXMLDOMDocumentPtr pXMLDoc;
	HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60));
	if(!SUCCEEDED(hr))
	{
		AfxMessageBox(_T("Failed to load msxml6. \n\
						 Please download it from www.caxsoft.com"));
		return false;
	}
	VARIANT_BOOL bOK = pXMLDoc->load(szPathFile);
	if(bOK == VARIANT_FALSE)
		return false;

	MSXML2::IXMLDOMElementPtr pRootNode = pXMLDoc->selectSingleNode(NODE_ROOT);
	if(NULL == pRootNode)
		return false;

	{
		MSXML2::IXMLDOMNodePtr pExporter = pRootNode->selectSingleNode(NODE_ALLEXPORTERS);
		if(pExporter)
		{
			MSXML2::IXMLDOMNodeListPtr pNodeList;
			pNodeList = pExporter->selectNodes(NODE_XPORTER);
			MSXML2::IXMLDOMNodePtr pNode;
			while(pNode=pNodeList->nextNode())
			{
				ParseXporter(pNode, m_Exporters);
			}
		}
	}


	{
		MSXML2::IXMLDOMNodePtr pImporter = pRootNode->selectSingleNode(NODE_ALLIMPORTERS);
		if(pImporter)
		{
			MSXML2::IXMLDOMNodeListPtr pNodeList;
			pNodeList = pImporter->selectNodes(NODE_XPORTER);
			MSXML2::IXMLDOMNodePtr pNode;
			while(pNode=pNodeList->nextNode())
			{
				ParseXporter(pNode, m_Importers);
			}
		}
	}



	return true;
}

bool CPlugsConfigure::ParseXporter(const MSXML2::IXMLDOMNodePtr& pNode, XportItemList& itemList)
{

	XportItem* pItem = new XportItem();
	itemList.push_back(pItem);

	// get editor name
	MSXML2::IXMLDOMNodePtr pItemNode = NULL;
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_NAME);
	if(pItemNode)
		pItem->szName = (LPTSTR)pItemNode->text;

	// get guid
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_GUID);
	if(pItemNode)
		pItem->guid = (LPTSTR)pItemNode->text;

	// get desc
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_DESC);
	if(pItemNode)
		pItem->szDesc = (LPTSTR)pItemNode->text;

	// get author
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_AUTHOR);
	if(pItemNode)
		pItem->szAuthor = (LPTSTR)pItemNode->text;

	// get dll
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_DLL);
	if(pItemNode)
		pItem->szDll = (LPTSTR)pItemNode->text;

	// get extention
	pItemNode = pNode->selectSingleNode(NODE_XPORTER_EXT);
	if(pItemNode)
		pItem->szExt = (LPTSTR)pItemNode->text;

	return true;
}